home *** CD-ROM | disk | FTP | other *** search
/ Hacker's Arsenal - The Cutting Edge of Hacking / Hacker's Arsenal - The Cutting Edge of Hacking.iso / texts / misc / IPspoofing.txt < prev    next >
Encoding:
Text File  |  2001-07-11  |  23.7 KB  |  495 lines

  1.                    [ IP-spoofing Demystified ]
  2.                       (Trust-Relationship Exploitation)
  3.  
  4.                         by daemon9 / route / infinity
  5.                              for Phrack Magazine
  6.                       June 1996 Guild Productions, kid
  7.  
  8.                        comments to route@infonexus.com
  9.  
  10.         The purpose of this paper is to explain IP-spoofing to the
  11. masses.  It assumes little more than a working knowledge of Unix and
  12. TCP/IP.  Oh, and that yur not a moron...
  13.         IP-spoofing is complex technical attack that is made up of
  14. several components.  (In actuality, IP-spoofing is not the attack, but
  15. a step in the attack.  The attack is actually trust-relationship
  16. exploitation.  However, in this paper,  IP-spoofing will refer to the
  17. whole attack.)  In this paper, I will explain the attack in detail,
  18. including the relevant operating system and networking information.
  19.  
  20.                 [SECTION I.  BACKGROUND INFORMATION]
  21.  
  22.         --[ The Players ]--
  23.  
  24.         A:      Target host
  25.         B:      Trusted host
  26.         X:      Unreachable host
  27.         Z:      Attacking host
  28.         (1)2:   Host 1 masquerading as host 2
  29.  
  30.         --[ The Figures ]--
  31.  
  32.         There are several figures in the paper and they are to be
  33. interpreted as per the following example:
  34.  
  35. ick   host a      control     host b
  36. 1       A       ---SYN--->      B
  37.  
  38. tick:   A tick of time.  There is no distinction made as to *how*
  39. much time passes between ticks, just that time passes.  It's generally
  40. not a great deal.
  41. host a: A machine particpating in a TCP-based conversation.
  42. control: This field shows any relevant control bits set in the TCP
  43. header and the direction the data is flowing
  44. host b: A machine particpating in a TCP-based conversation.
  45.  
  46. In this case, at the first refrenced point in time host a is sending
  47. a TCP segment to host b with the SYN bit on.  Unless stated, we are
  48. generally not concerned with the data portion of the TCP segment.
  49.  
  50.         --[ Trust Relationships ]--
  51.  
  52.         In the Unix world, trust can be given all too easily.  Say you
  53. have an account on machine A, and on machine B.  To facilitate going
  54. betwixt the two with a minimum amount of hassle, you want to setup a
  55. full-duplex trust relationship between them.  In your home directory
  56. at A you create a .rhosts file: `echo "B username" > ~/.rhosts` In
  57. your home directory at B you create a .rhosts file: `echo "A username"
  58. > ~/.rhosts` (Alternately, root can setup similar rules in
  59. /etc/hosts.equiv, the difference being that the rules are hostwide,
  60. rather than just on an individual basis.)  Now, you can use any of the
  61. r* commands without that annoying hassle of password authentication.
  62. These commands will allow address-based authentication, which will
  63. grant or deny access based off of the IP address of the service
  64. requestor.
  65.  
  66.         --[ Rlogin ]--
  67.  
  68.         Rlogin is a simple client-server based protocol that uses TCP
  69. as it's transport.  Rlogin allows a user to login remotely from one
  70. host to another, and, if the target machine trusts the other, rlogin
  71. will allow the convienience of not prompting for a password.  It will
  72. instead have authenticated the client via the source IP address.  So,
  73. from our example above, we can use rlogin to remotely login to A from
  74. B (or vice-versa) and not be prompted for a password.
  75.  
  76.         --[ Internet Protocol ]--
  77.  
  78.         IP is the connectionless, unreliable network protocol in the
  79. TCP/IP suite.  It has two 32-bit header fields to hold address
  80. information.  IP is also the busiest of all the TCP/IP protocols as
  81. almost all TCP/IP traffic is encapsulated in IP datagrams.  IP's job
  82. is to route packets around the network.  It provides no mechanism for
  83. reliability or accountability, for that, it relies on the upper
  84. layers.  IP simply sends out datagrams and hopes they make it intact.
  85. If they don't, IP can try to send an ICMP error message back to the
  86. source, however this packet can get lost as well.  (ICMP is Internet
  87. Control Message Protocol and it is used to relay network conditions
  88. and different errors to IP and the other layers.)  IP has no means to
  89. guarantee delivery.  Since IP is connectionless, it does not maintain
  90. any connection state information.  Each IP datagram is sent out without
  91. regard to the last one or the next one.  This, along with the fact that
  92. it is trivial to modify the IP stack to allow an arbitrarily choosen IP
  93. address in the source (and destination) fields make IP easily subvertable.
  94.  
  95.         --[ Transmission Control Protocol ]--
  96.  
  97.         TCP is the connection-oriented, reliable transport protocol
  98. in the TCP/IP suite.  Connection-oriented simply means that the two
  99. hosts participating in a discussion must first establish a connection
  100. before data may change hands.  Reliability is provided in a number of
  101. ways but the only two we are concerned with are data sequencing and
  102. acknowledgement.  TCP assigns sequence numbers to every segment and
  103. acknowledges any and all data segments recieved from the other end.
  104. (ACK's consume a sequence number, but are not themselves ACK'd.)
  105. This reliability makes TCP harder to fool than IP.
  106.  
  107.         --[ Sequence Numbers, Acknowledgements and other flags ]--
  108.  
  109.         Since TCP is reliable, it must be able to recover from
  110. lost, duplicated, or out-of-order data.  By assigning a sequence
  111. number to every byte transfered, and requiring an acknowledgement from
  112. the other end upon receipt, TCP can guarantee reliable delivery.  The
  113. receiving end uses the sequence numbers to ensure proper ordering of
  114. the data and to eliminate duplicate data bytes.
  115.         TCP sequence numbers can simply be thought of as 32-bit
  116. counters.  They range from 0 to 4,294,967,295.  Every byte of
  117. data exchanged across a TCP connection (along with certain flags)
  118. is sequenced.  The sequence number field in the TCP header will
  119. contain the sequence number of the *first* byte of data in the
  120. TCP segment.  The acknowledgement number field in the TCP header
  121. holds the value of next *expected* sequence number, and also
  122. acknowledges *all* data up through this ACK number minus one.
  123.         TCP uses the concept of window advertisement for flow
  124. control.  It uses a sliding window to tell the other end how much
  125. data it can buffer.  Since the window size is 16-bits a receiving TCP
  126. can advertise up to a maximum of 65535 bytes.  Window advertisement
  127. can be thought of an advertisment from one TCP to the other of how
  128. high acceptable sequence numbers can be.
  129.         Other TCP header flags of note are RST (reset), PSH (push)
  130. and FIN (finish).  If a RST is received, the connection is
  131. immediately torn down.  RSTs are normally sent when one end
  132. receives a segment that just doesn't jive with current connection
  133. (we will encounter an example below).  The PSH flag tells the
  134. reciever to pass all the data is has queued to the aplication, as
  135. soon as possible.  The FIN flag is the way an application begins a
  136. graceful close of a connection (connection termination is a 4-way
  137. process). When one end recieves a FIN, it ACKs it, and does not
  138. expect to receive any more data (sending is still possible, however).
  139.  
  140.         --[ TCP Connection Establishment ]--
  141.  
  142.         In order to exchange data using TCP, hosts must establish a
  143. a connection.  TCP establishes a connection in a 3 step process called
  144. the 3-way handshake.  If machine A is running an rlogin client and
  145. wishes to conect to an rlogin daemon on machine B, the process is as
  146. follows:
  147.  
  148.                 fig(1)
  149.  
  150. 1       A       ---SYN--->      B
  151.  
  152. 2       A    <---SYN/ACK---     B
  153.  
  154. 3       A       ---ACK--->      B
  155.  
  156. At (1) the client is telling the server that it wants a connection.
  157. This is the SYN flag's only purpose.  The client is telling the
  158. server that the sequence number field is valid, and should be checked.
  159. The client will set the sequence number field in the TCP header to
  160. it's ISN (initial sequence number).  The server, upon receiving this
  161. segment (2) will respond with it's own ISN (therefore the SYN flag is
  162. on) and an ACKnowledgement of the clients first segment (which is the
  163. client's ISN+1).  The client then ACK's the server's ISN (3).  Now,
  164. data transfer may take place.
  165.  
  166.         --[ The ISN and Sequence Number Incrementation ]--
  167.  
  168.         It is important to understand how sequence numbers are
  169. initially choosen, and how they change with respect to time.  The
  170. initial sequence number when a host is bootstraped is initialized
  171. to 1. (TCP actually calls this variable 'tcp_iss' as it is the initial
  172. *send* sequence number.  The other sequence number variable,
  173. 'tcp_irs' is the initial *receive* sequence number and is learned
  174. during the 3-way connection establishment.  We are not going to worry
  175. about the distinction.)  This practice is wrong, and is acknowledged
  176. as so in a comment the tcp_init() function where it appears.  The ISN
  177. is incremented by 128,000 every second, which causes the 32-bit ISN
  178. counter to wrap every 9.32 hours if no connections occur.  However,
  179. each time a connect() is issued, the counter is incremented by
  180. 64,000.
  181.         One important reason behind this predictibility is to
  182. minimize the chance that data from an older stale incarnation
  183. (that is, from the same 4-tuple of the local and remote
  184. IP-addresses TCP ports) of the current connection could arrive
  185. and foul things up.  The concept of the 2MSL wait time applies
  186. here, but is beyond the scope of this paper.  If sequence
  187. numbers were choosen at random when a connection arrived, no
  188. guarantees could be made that the sequence numbers would be different
  189. from a previous incarnation.  If some data that was stuck in a
  190. routing loop somewhere finally freed itself and wandered into the new
  191. incarnation of it's old connection, it could really foul things up.
  192.  
  193.         --[ Ports ]--
  194.  
  195.         To grant simultaneous access to the TCP module, TCP provides
  196. a user interface called a port.  Ports are used by the kernel to
  197. identify network processes.  These are strictly transport layer
  198. entities (that is to say that IP could care less about them).
  199. Together with an IP address, a TCP port provides provides an endpoint
  200. for network communications.  In fact, at any given moment *all*
  201. Internet connections can be described by 4 numbers: the source IP
  202. address and source port and the destination IP address and destination
  203. port.  Servers are bound to 'well-known' ports so that they may be
  204. located on a standard port on different systems.  For example, the
  205. rlogin daemon sits on TCP port 513.
  206.  
  207.                 [SECTION II.  THE ATTACK]
  208.  
  209.         ...The devil finds work for idle hands....
  210.  
  211.         --[ Briefly... ]--
  212.  
  213.         IP-spoofing consists of several steps, which I will
  214. briefly outline here, then explain in detail.  First, the target host
  215. is choosen.  Next, a pattern of trust is discovered, along with a
  216. trusted host.  The trusted host is then disabled, and the target's TCP
  217. sequence numbers are sampled.  The trusted host is impersonated, the
  218. sequence numbers guessed, and a connection attempt is made to a
  219. service that only requires address-based authentication.  If
  220. successful, the attacker executes a simple command to leave a
  221. backdoor.
  222.  
  223.         --[ Needful Things ]--
  224.  
  225.         There are a couple of things one needs to wage this attack:
  226.  
  227.                 (1) brain, mind, or other thinking device
  228.                 (1) target host
  229.                 (1) trusted host
  230.                 (1) attacking host (with root access)
  231.                 (1) IP-spoofing software
  232.  
  233. Generally the attack is made from the root account on the attacking
  234. host against the root account on the target.  If the attacker is
  235. going to all this trouble, it would be stupid not to go for root.
  236. (Since root access is needed to wage the attack, this should not
  237. be an issue.)
  238.  
  239.         --[ IP-Spoofing is a 'Blind Attack' ]--
  240.  
  241.         One often overlooked, but critical factor in IP-spoofing
  242. is the fact that the attack is blind.  The attacker is going to be
  243. taking over the identity of a trusted host in order to subvert the
  244. security of the target host.  The trusted host is disabled using the
  245. method described below.  As far as the target knows, it is carrying on
  246. a conversation with a trusted pal.  In reality, the attacker is
  247. sitting off in some dark corner of the Internet, forging packets
  248. puportedly from this trusted host while it is locked up in a denial
  249. of service battle.  The IP datagrams sent with the forged IP-address
  250. reach the target fine (recall that IP is a connectionless-oriented
  251. protocol--  each datagram is sent without regard for the other end)
  252. but the datagrams the target sends back (destined for the trusted
  253. host) end up in the bit-bucket.  The attacker never sees them.  The
  254. intervening routers know where the datagrams are supposed to go.  They
  255. are supposed to go the trusted host.  As far as the network layer is
  256. concerned, this is where they originally came from, and this is where
  257. responses should go.  Of course once the datagrams are routed there,
  258. and the information is demultiplexed up the protocol stack, and
  259. reaches TCP, it is discarded (the trusted host's TCP cannot respond--
  260. see below).  So the attacker has to be smart and *know* what was sent,
  261. and *know* what reponse the server is looking for.  The attacker
  262. cannot see what the target host sends, but she can *predict* what it
  263. will send; that coupled with the knowledge of what it *will* send,
  264. allows the attacker to work around this blindness.
  265.  
  266.         --[ Patterns of Trust ]--
  267.  
  268.         After a target is choosen the attacker must determine the
  269. patterns of trust (for the sake of argument, we are going to assume
  270. the target host *does* in fact trust somebody.  If it didn't, the
  271. attack would end here).  Figuring out who a host trusts may or may
  272. not be easy.  A 'showmount -e' may show where filesystems are
  273. exported, and rpcinfo can give out valuable information as well.
  274. If enough background information is known about the host, it should
  275. not be too difficult.  If all else fails, trying neighboring IP
  276. addresses in a brute force effort may be a viable option.
  277.  
  278.         --[ Trusted Host Disabling Using the Flood of Sins ]--
  279.  
  280.         Once the trusted host is found, it must be disabled.  Since
  281. the attacker is going to impersonate it, she must make sure this host
  282. cannot receive any network traffic and foul things up.  There are
  283. many ways of doing this, the one I am going to discuss is TCP SYN
  284. flooding.
  285.         A TCP connection is initiated with a client issuing a
  286. request to a server with the SYN flag on in the TCP header.  Normally
  287. the server will issue a SYN/ACK back to the client identified by the
  288. 32-bit source address in the IP header.  The client will then send an
  289. ACK to the server (as we saw in figure 1 above) and data transfer
  290. can commence.  There is an upper limit of how many concurrent SYN
  291. requests TCP can process for a given socket, however.  This limit
  292. is called the backlog, and it is the length of the queue where
  293. incoming (as yet incomplete) connections are kept.  This queue limit
  294. applies to both the number of imcomplete connections (the 3-way
  295. handshake is not complete) and the number of completed connections
  296. that have not been pulled from the queue by the application by way of
  297. the accept() system call.  If this backlog limit is reached, TCP will
  298. silently discard all incoming SYN requests until the pending
  299. connections can be dealt with.  Therein lies the attack.
  300.         The attacking host sends several SYN requests to the TCP port
  301. she desires disabled.  The attacking host also must make sure that
  302. the source IP-address is spoofed to be that of another, currently
  303. unreachable host (the target TCP will be sending it's response to
  304. this address.  (IP may inform TCP that the host is unreachable,
  305. but TCP considers these errors to be transient and leaves the
  306. resolution of them up to IP (reroute the packets, etc) effectively
  307. ignoring them.)  The IP-address must be unreachable because the
  308. attacker does not want any host to recieve the SYN/ACKs that will be
  309. coming from the target TCP (this would result in a RST being sent to
  310. the target TCP, which would foil our attack).  The process is as
  311. follows:
  312.  
  313.                 fig(2)
  314.  
  315. 1       Z(x)    ---SYN--->      B
  316.  
  317.         Z(x)    ---SYN--->      B
  318.  
  319.         Z(x)    ---SYN--->      B
  320.  
  321.         Z(x)    ---SYN--->      B
  322.  
  323.         Z(x)    ---SYN--->      B
  324.  
  325.                 ...
  326.  
  327. 2       X    <---SYN/ACK---     B
  328.  
  329.         X    <---SYN/ACK---     B
  330.  
  331.                 ...
  332.  
  333. 3       X      <---RST---       B
  334.  
  335. At (1) the attacking host sends a multitude of SYN requests to the
  336. target (remember the target in this phase of the attack is the
  337. trusted host) to fill it's backlog queue with pending connections.
  338. (2) The target responds with SYN/ACKs to what it believes is the
  339. source of the incoming SYNs.  During this time all further requests
  340. to this TCP port will be ignored.
  341.         Different TCP implementations have different backlog sizes.
  342. BSD generally has a backlog of 5 (Linux has a backlog of 6).  There
  343. is also a 'grace' margin of 3/2.  That is, TCP will allow up to
  344. backlog*3/2+1 connections.  This will allow a socket one connection
  345. even if it calls listen with a backlog of 0.
  346.  
  347.         AuthNote: [For a much more in-depth treatment of TCP SYN
  348. flooding, see my definitive paper on the subject.  It covers the
  349. whole process in detail, in both theory, and practice.  There is
  350. robust working code, a statistical analysis, and a legnthy paper.
  351. Look for it in issue 49 of Phrack. -daemon9 6/96]
  352.  
  353.         --[ Sequence Number Sampling and Prediction ]--
  354.  
  355.         Now the attacker needs to get an idea of where in the 32-bit
  356. sequence number space the target's TCP is.  The attacker connects to
  357. a TCP port on the target (SMTP is a good choice) just prior to launching
  358. the attack and completes the three-way handshake.  The process is
  359. exactly the same as fig(1), except that the attacker will save the
  360. value of the ISN sent by the target host.  Often times, this process is
  361. repeated several times and the final ISN sent is stored.  The attacker
  362. needs to get an idea of what the RTT (round-trip time) from the target
  363. to her host is like.  (The process can be repeated several times, and an
  364. average of the RTT's is calculated.)  The RTT is necessary in being
  365. able to accuratly predict the next ISN.  The attacker has the baseline
  366. (the last ISN sent) and knows how the sequence numbers are incremented
  367. (128,000/second and 64,000 per connect) and now has a good idea of
  368. how long it will take an IP datagram to travel across the Internet to
  369. reach the target (approximately half the RTT, as most times the
  370. routes are symmetrical).  After the attacker has this information, she
  371. immediately proceeds to the next phase of the attack (if another TCP
  372. connection were to arrive on any port of the target before the
  373. attacker was able to continue the attack, the ISN predicted by the
  374. attacker would be off by 64,000 of what was predicted).
  375.         When the spoofed segment makes it's way to the target,
  376. several different things may happen depending on the accuracy of
  377. the attacker's prediction:
  378. - If the sequence number is EXACTly where the receiving TCP expects
  379. it to be, the incoming data will be placed on the next available
  380. position in the receive buffer.
  381. - If the sequence number is LESS than the expected value the data
  382. byte is considered a retransmission, and is discarded.
  383. - If the sequence number is GREATER than the expected value but
  384. still within the bounds of the receive window, the data byte is
  385. considered to be a future byte, and is held by TCP, pending the
  386. arrival of the other missing bytes.  If a segment arrives with a
  387. sequence number GREATER than the expected value and NOT within the
  388. bounds of the receive window the segment is dropped, and TCP will
  389. send a segment back with the *expected* sequence number.
  390.  
  391.         --[ Subversion... ]--
  392.  
  393.         Here is where the main thrust of the attack begins:
  394.  
  395.                 fig(3)
  396.  
  397. 1       Z(b)    ---SYN--->      A
  398.  
  399. 2       B     <---SYN/ACK---    A
  400.  
  401. 3       Z(b)    ---ACK--->      A
  402.  
  403. 4       Z(b)    ---PSH--->      A
  404.  
  405.                 [...]
  406.  
  407. The attacking host spoofs her IP address to be that of the trusted
  408. host (which should still be in the death-throes of the D.O.S. attack)
  409. and sends it's connection request to port 513 on the target (1).  At
  410. (2), the target responds to the spoofed connection request with a
  411. SYN/ACK, which will make it's way to the trusted host (which, if it
  412. *could* process the incoming TCP segment, it would consider it an
  413. error, and immediately send a RST to the target).  If everything goes
  414. according to plan, the SYN/ACK will be dropped by the gagged trusted
  415. host.  After (1), the attacker must back off for a bit to give the
  416. target ample time to send the SYN/ACK (the attacker cannot see this
  417. segment).  Then, at (3) the attacker sends an ACK to the target with
  418. the predicted sequence number (plus one, because we're ACKing it).
  419. If the attacker is correct in her prediction, the target will accept
  420. the ACK.  The target is compromised and data transfer can
  421. commence (4).
  422.         Generally, after compromise, the attacker will insert a
  423. backdoor into the system that will allow a simpler way of intrusion.
  424. (Often a `cat + + >> ~/.rhosts` is done.  This is a good idea for
  425. several reasons: it is quick, allows for simple re-entry, and is not
  426. interactive.  Remember the attacker cannot see any traffic coming from
  427. the target, so any reponses are sent off into oblivion.)
  428.  
  429.         --[ Why it Works ]--
  430.  
  431.         IP-Spoofing works because trusted services only rely on
  432. network address based authentication.  Since IP is easily duped,
  433. address forgery is not difficult.  The hardest part of the attck is
  434. in the sequence number prediction, because that is where the guesswork
  435. comes into play.  Reduce unknowns and guesswork to a minimum, and
  436. the attack has a better chance of suceeding.  Even a machine that
  437. wraps all it's incoming TCP bound connections with Wietse Venema's TCP
  438. wrappers, is still vulnerable to the attack.  TCP wrappers rely on a
  439. hostname or an IP address for authentication...
  440.  
  441.                 [SECTION III. PREVENTITIVE MEASURES]
  442.  
  443.         ...A stich in time, saves nine...
  444.  
  445.         --[ Be Un-trusting and Un-trustworthy ]--
  446.  
  447.         One easy solution to prevent this attack is not to rely
  448. on address-based authentication.  Disable all the r* commands,
  449. remove all .rhosts files and empty out the /etc/hosts.equiv file.
  450. This will force all users to use other means of remote access
  451. (telnet, ssh, skey, etc).
  452.  
  453.         --[ Packet Filtering ]--
  454.  
  455.         If your site has a direct connect to the Internet, you
  456. can use your router to help you out.  First make sure only hosts
  457. on your internal LAN can particpate in trust-relationships (no
  458. internal host should trust a host outside the LAN).  Then simply
  459. filter out *all* traffic from the outside (the Internet) that
  460. puports to come from the inside (the LAN).
  461.  
  462.         --[ Cryptographic Methods ]--
  463.  
  464.         An obvious method to deter IP-spoofing is to require
  465. all network traffic to be encrypted and/or authenticated.  While
  466. several solutions exist, it will be a while before such measures are
  467. deployed as defacto standards.
  468.  
  469.         --[ Initial Sequence Number Randomizing ]--
  470.  
  471.         Since the sequence numbers are not choosen randomly (or
  472. incremented randomly) this attack works.  Bellovin describes a
  473. fix for TCP that involves partitioning the sequence number space.
  474. Each connection would have it's own seperate sequence number space.
  475. The sequence numbers would still be incremented as before, however,
  476. there would be no obvious or implied relationship between the
  477. numbering in these spaces.  Suggested is the following formula:
  478.  
  479.         ISN=M+F(localhost,localport,remotehost,remoteport)
  480.  
  481. Where M is the 4 microsecond timer and F is a cryptographic hash.
  482. F must not be computable from the outside or the attacker could
  483. still guess sequence numbers.  Bellovin suggests F be a hash of
  484. the connection-id and a secret vector (a random number, or a host
  485. related secret combined with the machine's boot time).
  486.  
  487.                 [SECTION IV.  SOURCES]
  488.  
  489.         -Books:         TCP/IP Illustrated vols. I, II & III
  490.         -RFCs:          793, 1825, 1948
  491.         -People:        Richard W. Stevens, and the users of the
  492.                         Information Nexus for proofreading
  493.         -Sourcecode:    rbone, mendax, SYNflood
  494.  
  495. This paper made possible by a grant from the Guild Corporation.